//
//  CarouseView2.m
//  TotalDemo
//
//  Created by gujian on 16/5/17.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "CarouseView2.h"

static const CGFloat marginLeft = 15.0;
static const CGFloat kCarousHeight = 34.0;

@interface CarouseView2()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (nonatomic, strong) NSArray<UIView *> *viewsArr;
@property (nonatomic, assign) NSInteger currentPageIndex;
@property (nonatomic, assign) CGFloat preDeltaX;
@property (nonatomic, assign) CGFloat preOffX;

@end

@implementation CarouseView2

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _currentPageIndex = 0;
        [self setupSubView];
    }
    
    return self;
}

- (void)setupSubView
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    [scrollView setScrollsToTop:NO];
    [scrollView setAlwaysBounceHorizontal:NO];
    [scrollView setAlwaysBounceVertical:NO];
    [scrollView setBounces:NO];
    [self addSubview:scrollView];
    _contentScrollView = scrollView;
    
    NSArray *colorArr = @[[UIColor redColor],[UIColor grayColor],[UIColor magentaColor]];
    
    CGFloat viewWidth = self.width- 2 * marginLeft;
    CGFloat offX = marginLeft;
    NSMutableArray *tempArr = [NSMutableArray arrayWithCapacity:colorArr.count];
    for (int i = 0; i < 2; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(offX, 0, viewWidth, self.height)];
        if (i == 1) {
            
        }
        
        switch (i) {
            case 0:
                
                break;
            case 1:
//                view.layer.transform = CATransform3DScale(view.layer.transform, 0.8, 0.8, 1.0);
//                view.layer.transform = CATransform3DTranslate(view.layer.transform, self.width-2*marginLeft, 0, -100);
                break;
            case 2:
                view.layer.transform = CATransform3DTranslate(CATransform3DIdentity, 100, 10, -10);
                break;
            default:
                break;
        }
        
        view.centerY = scrollView.height/2.0;
        view.backgroundColor = colorArr[i];
        
        [scrollView addSubview:view];
//        offX += self.width;
        NSLog(@"offX:%@",@(offX));
        view.alpha = 0.7;

        [tempArr addObject:view];
    }
    
    _viewsArr = [tempArr copy];
    scrollView.contentSize = CGSizeMake(viewWidth * colorArr.count, 0);
    
}





#pragma mark - UIScrollView delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
//    [self updateScrollView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    NSInteger pageIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
//    if (pageIndex != _currentPageIndex) {
//        _currentPageIndex = pageIndex;
//        [self updateScrollView:pageIndex];
//    }
}

//最终形态
- (void)updateScrollView:(NSInteger )pageIndex
{
    
    
}

//滑动中的形态
- (void)updateScrollView
{
    UIView *preView =  nil;
    UIView *nextView = nil;
    NSInteger pageIndex = _contentScrollView.contentOffset.x / (self.width);
    CGFloat dif = _contentScrollView.contentOffset.x - _preDeltaX;
    if (pageIndex > 0) {
        preView = _viewsArr[pageIndex-1];
    }
    UIView *currentView = _viewsArr[pageIndex];
    
    //修改current的left

    
    
    if (pageIndex < (_viewsArr.count-1)) {
        nextView = _viewsArr[pageIndex + 1];
        if (dif >= 0 && nextView.left < currentView.right) {
            nextView.left += dif;
        }else{
            nextView.left -= dif;
        }
    }
    
    _preDeltaX = _contentScrollView.contentOffset.x;
    
}



@end
